home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / CWL2A_1.ARJ / MENUDEMO.C < prev    next >
Text File  |  1991-11-16  |  18KB  |  538 lines

  1. /* Date 11-1-91 */
  2.  
  3.  
  4.  
  5. /************************************************************************
  6.  * This is the demonstration program of the menu functions available    *
  7.  * with The C Window Library.  Uses a mouse if mouse is installed.      *
  8.  *                                                                      *
  9.  * For Turbo C version 1.5 and above,  compile using the following:     *
  10.  *     tcc -mm -Ixxx menudemo.c tcwinm.lib tcmousem.lib                 *
  11.  * where xxx is the directory of The C Window Library header files.     *
  12.  *                                                                      *
  13.  * For Microsoft C users Version 5.1 and above:                         *
  14.  * cl -AM -DMSC -Ixxx menudemo /link /SE:1000 mswinm.lib msmousem.lib   *
  15.  * (use "qcl" instead of "cl" if using Quick C)                         *
  16.  * where xxx is the directory of The C Window Library header files.     *
  17.  *                                                                      *
  18.  * For Zortech C++ Version 2.0 and above:                               *
  19.  *     ztc -mm -p -b -Ixxx menudemo.c zcwinm.lib zcmousem.lib           *
  20.  * where xxx is the directory of The C Window Library header files.     *
  21.  *                                                                      *
  22.  * For Power C Version 2.1 and above:                                   *
  23.  *     pc -DPOWERC -mm -c -ixxx menudemo.c                              *
  24.  *     pcl [10k,30k,300k] menudemo;pcwinm;pcmousem                      *
  25.  * where xxx is the directory of The C Window Library header files.     *
  26.  ************************************************************************/
  27.  
  28.  
  29. #include "menu.h"
  30. #include "cwlmouse.h"
  31.  
  32. /* define colors */
  33.  
  34. #define YELLOWONRED    CREATE_VIDEO_ATTRIBUTE(red,yellow)
  35. #define WHITEONBLUE    CREATE_VIDEO_ATTRIBUTE(blue,white)
  36. #define BLUEONWHITE    CREATE_VIDEO_ATTRIBUTE(white,blue)
  37. #define BLACKONCYAN    CREATE_VIDEO_ATTRIBUTE(cyan,black)
  38. #define NORM           CREATE_VIDEO_ATTRIBUTE(black,white)
  39. #define BLACKONWHITE   CREATE_VIDEO_ATTRIBUTE(white,black)
  40. #define WHITEONRED     CREATE_VIDEO_ATTRIBUTE(red,lightwhite)
  41.  
  42.  
  43. #define INTROLINES  6
  44.  
  45. WPOINTER help,pw,intro,error_window;
  46. VBLOCKPTR v;
  47.  
  48.  
  49. int rt1();      /* Routine that is called when popup option is chosen */
  50. int rt2();      /* Routine that is called when bar option is chosen */
  51. int rt3();      /* Routine that is called when pulldown option is chosen */
  52. int beep_it();  /* Routine that is called when undefined key is pressed */
  53. void bar_demo();      /* Routine to do bar menu demo */
  54. void popup_demo();    /* Routine to do popup demo */
  55. void pulldown_demo(); /* Routine to do pulldown menu demo */
  56.  
  57. void custom_error_func();
  58. void initialize_error();
  59.  
  60. int error_check_flag = 1;          /* Turn error checking on (not to be
  61.                                     * confused with the internal variable
  62.                                     * check_window_error) */
  63.  
  64. char *intro_lines[] =
  65.                       {"This is a demo of the menu functions",
  66.                        "available with The C Window Library.",
  67.                        "If you have a mouse, you can use the",
  68.                        "mouse to make menu selections.      ",
  69.                        "    ",
  70.                        "     Press a key to continue..."};
  71.  
  72.  
  73. /*****************************
  74.  * Define popup menu entries *
  75.  ****************************/
  76.  
  77. POPUP_MENU_ENTRY menu_items1[] = {
  78.                                   " Load      F3", /* option name */
  79.                                     1,             /* row number */
  80.                                    'L',            /* hotkey */
  81.                                     F3,            /* secondary hotkey */
  82.                                     rt1,      /* function to call if chosen */
  83.  
  84.                                  /* now do rest of the popup entries */
  85.                                   " Pick  Alt-F3",2, 'P',ALTF3,rt1,
  86.                                   " New"         ,3, 'N',0,rt1,
  87.                                   " Save      F2",4, 'S',F2,rt1,
  88.                                   " Write to    ",5, 'W',0,rt1,
  89.                                   " Directory   ",6, 'D',0,rt1,
  90.                                   " Change Dir"  ,7, 'C',0,rt1,
  91.                                   " OS shell"    ,8, 'O',0,rt1,
  92.                                   " Quit  Alt-X"  ,9,'Q',ALTX,rt1,
  93.  
  94.  
  95.                                  /* Terminate with a NULL and 0 */
  96.  
  97.                                     CWL_NULL,0};
  98.  
  99.  
  100.  
  101. POPUP_MENU_ENTRY menu_items3[] = {
  102.                       " Run             Ctrl-F9 ",1,'R',CTRLF9,rt3,
  103.                       " Program reset   Ctrl-F2 ",2,'P',CTRLF2,rt3,
  104.                       " Go to Cursor         F4 ",3,'G',F4,rt3,
  105.                       " Trace into           F7 ",4,'T',F7,rt3,
  106.                       " Step over            F8 ",5,'S',F8,rt3,
  107.                       " User screen      Alt-F5 ",6,'U',ALTF5,rt3,
  108.                         CWL_NULL,0};
  109.  
  110.  
  111. POPUP_MENU_ENTRY menu_items4[] = {
  112.         " Compile to OBJ                   ",1,'C',0,rt3,
  113.         " Make EXE file                    ",2,'M',0,rt3,
  114.         " Link EXE file                    ",3,'L',0,rt3,
  115.         " Build all                        ",4,'B',0,rt3,
  116.         " Primary C file:                  ",5,'P',0,rt3,
  117.         " Get info                         ",6,'G',0,rt3,
  118.       CWL_NULL,0};
  119.  
  120.  
  121. POPUP_MENU_ENTRY menu_items5[] = {
  122.         " Project Name                     ",1,'P',0,rt3,
  123.         " Break make on                    ",2,'B',0,rt3,
  124.         " Auto dependencies                ",3,'A',0,rt3,
  125.         " Clear project                    ",4,'C',0,rt3,
  126.         " Remove messages                  ",5,'R',0,rt3,
  127.           CWL_NULL,0};
  128.  
  129.  
  130. POPUP_MENU_ENTRY menu_items6[] = {
  131.         " Compiler          ",1,'C',0,rt3,
  132.         " Linker            ",2,'L',0,rt3,
  133.         " Environment       ",3,'E',0,rt3,
  134.         " Directories       ",4,'D',0,rt3,
  135.         " Arguments         ",5,'A',0,rt3,
  136.         " Save Options      ",6,'S',0,rt3,
  137.         " Retrieve options  ",7,'R',0,rt3,
  138.       CWL_NULL,0};
  139.  
  140.  
  141. POPUP_MENU_ENTRY menu_items7[] = {
  142.         " Evaluate    Ctrl-F4           ",1,'E',CTRLF4,rt3,
  143.         " Call Stack  Ctrl-F3           ",2,'C',CTRLF3,rt3,
  144.         " Find Function                 ",3,'F',0,rt3,
  145.         " Refresh display               ",4,'R',0,rt3,
  146.         " Display swapping     Smart    ",5,'D',0,rt3,
  147.         " Source debugging              ",6,'S',0,rt3,
  148.           CWL_NULL,0};
  149.  
  150.  
  151. POPUP_MENU_ENTRY menu_items8[] = {
  152.         " Add watch           Ctrl-F7 ",1,'A',CTRLF7,rt3,
  153.     " Delete watch                ",2,'D',0,rt3,
  154.         " Edit Watch                  ",3,'E',0,rt3,
  155.         " Remove all watches          ",4,'R',0,rt3,
  156.         " Toggle breakpoint   Ctrl-F8 ",6,'T',CTRLF8,rt3,
  157.         " Clear all breakpoints       ",7,'C',0,rt3,
  158.         " View next breakpoint        ",8,'V',0,rt3,
  159.           CWL_NULL,0};
  160.  
  161.  
  162.  
  163.  
  164.  
  165. /**********************
  166.  * Define Bar entries *
  167.  *********************/
  168.  
  169. BAR_MENU_ENTRY bar_items[] = {
  170.       "File",       1,4, 'F',0,rt2,
  171.       "Edit",       1,10,'E',0,rt2,
  172.       "Run",        1,17,'R',0,rt2,
  173.       "Compile",    1,23,'C',0,rt2,
  174.       "Project",    1,33,'P',0,rt2,
  175.       "Options",    1,43,'O',0,rt2,
  176.       "Debug",      1,53,'D',0,rt2,
  177.       "Break/watch",1,61,'B',0,rt2,
  178.                   CWL_NULL};
  179.  
  180.  
  181. /* Define array of POPUP_MENU_ENTRY's used for pulldown menu */
  182. /* This array is used for convenience when initializing all of the
  183.  * POPUP_MENU_PTR's               */
  184.  
  185. POPUP_MENU_ENTRY  *pentry[] = {menu_items1, POPUP_ENTRY_NULL, menu_items3,
  186.                    menu_items4, menu_items5, menu_items6,
  187.                    menu_items7, menu_items8};
  188.  
  189.  
  190. unsigned menu_colors[5];    /* Menu colors */
  191. BAR_MENU_PTR bar;           /* Pointer to bar menu */
  192. POPUP_MENU_PTR popups[8];   /* Array of pointers to pop-up menus */
  193. PULLDOWN_MENU_PTR pull;     /* Pointer to pulldown menu */
  194.  
  195. WPOINTER my_open();         /* Pointer to custom bar window open function */
  196. int popuprank[] = {1,1,1,1,1,1,1,1};  /* Ranks of pop-up windows */
  197. unsigned popupstart[] = {1,1,1,1,1,1,1,1}; /* Starting option for each pop-up
  198.                           menu */
  199. int height[] = {9,0,6,6,5,7,6,8};     /* height of each window */
  200.  
  201.  
  202.  
  203. char *line =
  204.     "─────────────────────────────"; /* Horizontal line */
  205.  
  206. int which,hpos,vpos;
  207.  
  208. void main()
  209. {
  210.   int i,d,b;
  211.   /* Get Choice of which screen method to use */
  212.   printf("\nPlease enter: '0' for Direct Screen Writes\n"
  213.      "              '1' for Direct Screen Writes with snow checking\n"
  214.    "              '2' for BIOS Screen updates: ");
  215.   scanf("%d",&d);
  216.   WindowInitializeSystem();
  217.   CHECK_SNOW = d;
  218.   SCREEN_WRITE_METHOD = (d == 2)?BIOS:DMA;
  219.  
  220.   /* Save Initial Screen as a VBLOCK */
  221.   v = VideoSave(1,1,num_screen_rows,num_screen_cols);
  222.   ClearScreen(NORM);
  223.  
  224.   /* Save Base Screen as a window */
  225.   WindowSaveInitial(active_video_page);
  226.  
  227.   /* Initialize error function */
  228.   initialize_error();
  229.  
  230.   intro = WindowInitialize(BORDER,1,1,36,INTROLINES,NORM,NORM,SINGLEBOX);
  231.   WindowOpen(intro);
  232.   /* Write introduction to window */
  233.   WindowCenter(intro,HORIZCENTER | VERTCENTER);
  234.   for (i=1;i<=INTROLINES;i++)
  235.     WindowWriteString(intro,intro_lines[i-1],i,1);
  236.  
  237.   WindowDisplay(intro,1,EXPLODE);
  238.   GET_MPRESS_KEY(&which);
  239.   WindowFree(intro,CONTRACT);
  240.  
  241.   /* Initialize mouse */
  242.   MouseInitializeSystem(MOUSE_FULL_INSTALL,&b,1,1);
  243.  
  244.   /* Initialize Info Window */
  245.   pw = WindowInitialize(BORDER,19,10,50,4,CREATE_VIDEO_ATTRIBUTE(black,white),
  246.           CREATE_VIDEO_ATTRIBUTE(black,white), SINGLEBOX);
  247.  
  248.   /* Open Info Window */
  249.   WindowOpen(pw);
  250.  
  251.   /* Initialize And Open Help Window */
  252.   help = WindowInitialize(NOBORDER,25,1,80,1,BLACKONWHITE,BLACKONWHITE,0);
  253.   WindowOpen(help);
  254.  
  255.   /* Write Help Info to help window */
  256.   WindowWriteCenterString(help,
  257.   "Press \030 or \031 or use mouse to move bar,   ─┘ or hotkey to accept,   ESC to exit",1);
  258.  
  259.  /* define colors */
  260.   menu_colors[ENTRYCOLOR] =
  261.   menu_colors[UNAVAILCOLOR] =
  262.   menu_colors[BORDERCOLOR] = CREATE_VIDEO_ATTRIBUTE(white,black);
  263.  
  264.   menu_colors[HOTKEYCOLOR] =
  265.   menu_colors[HIGHLIGHTCOLOR] = CREATE_VIDEO_ATTRIBUTE(black,white);
  266.  
  267.   /* Call Popup Demo */
  268.   popup_demo();
  269.  
  270.   /* Call Bar Demo */
  271.   bar_demo();
  272.  
  273.   /* Call Pulldown Demo */
  274.   pulldown_demo();
  275.  
  276.   /* Restore Initial Screen */
  277.   VideoRestore(v);
  278.  
  279.   /* Turn off mouse */
  280.   MouseRestoreSystem();
  281. }
  282.  
  283.  
  284. void popup_demo()
  285. {
  286.   POPUP_MENU_PTR p;
  287.  
  288.  /* create a POPUP_MENU_PTR */
  289.   p = PopupCreateMenu(menu_items1,menu_colors, 1,31,9,
  290.                       POPUPSTATIC | POPUPMOUSE | POPUPWRAP | POPUPMOUSESCROLL,
  291.                       WNULLFN,VWNULLFN);
  292.  
  293.  /* Define routine to do when undefined key is pressed */
  294.   popup_undef_key = beep_it;
  295.  
  296.  /* Display the help window */
  297.   WindowDisplay(help,1,NOEFFECT);
  298.  
  299.  /* Call Selection Function */
  300.   PopupSelectMenu(p,  /* POPUP_MENU_PTR */
  301.            1,  /* rank of pop-up window */
  302.            1   /* menu option to start on */
  303.           );
  304.  
  305.  /* Dispose of popup menu */
  306.   PopupMenuFree(p);
  307. }
  308.  
  309.  
  310.  
  311.  /* Routine that is called when popup function is invoked */
  312.  
  313. int rt1(POPUP_MENU_PTR p, int which)
  314. {
  315.   WindowClear(pw);
  316.  
  317.  /* Display the entry name that was chosen */
  318.   WindowPrintf(pw,"You have selected %s",POPUP_ENTRY_STRING(p,which));
  319.  
  320.   WindowWriteCenterString(pw,"Press a key to continue",4);
  321.  
  322.  /* Display the window with all the info */
  323.   WindowDisplay(pw,1,NOEFFECT);
  324.   GET_MPRESS_KEY(&which);
  325.  
  326.  /* Hide the info window */
  327.   WindowHide(pw,NOEFFECT);
  328.  
  329.  /* If "Quit   Alt-X" was selected, then get out of popup menu */
  330.   if (which == 9)
  331.     return POPUP_EXIT;
  332.  
  333.  /* Keep getting popup selections */
  334.   else
  335.   return POPUP_CONTINUE;
  336. }
  337.  
  338.  
  339. void bar_demo()
  340. {
  341.   BAR_MENU_PTR b;
  342.  
  343.  /* Create Bar Menu */
  344.   b = BarCreateMenu(bar_items, menu_colors,1,1,BARWRAP|BARSTATIC|BARMOUSE,
  345.                     WNULLFN);
  346.  
  347.  /* Assign routine to call when undefined key is pressed */
  348.   bar_undef_key = beep_it;
  349.  
  350.  /* Clear and rewrite help info in help window */
  351.   WindowClear(help);
  352.   WindowWriteCenterString(help,
  353.   "Press \032 or \033 or use mouse to move bar   ─┘ or hotkey to accept   ESC to exit",1);
  354.  
  355.  /* Call Bar Selection function */
  356.   BarSelectMenu(b,        /* BAR_MENU_PTR */
  357.         1,        /* Rank of bar window */
  358.         1);       /* Starting Entry */
  359.  
  360.  /* Dispose of Bar Menu */
  361.   BarMenuFree(b);
  362. }
  363.  
  364.  
  365. int rt2(BAR_MENU_PTR b, int which)
  366. {
  367.   WindowClear(pw);
  368.  
  369.   /* Display the entry name that was chosen */
  370.   WindowPrintf(pw,"You have selected %s",BAR_ENTRY_STRING(b,which));
  371.   WindowWriteCenterString(pw,"Press a key to continue",4);
  372.   WindowDisplay(pw,1,NOEFFECT);
  373.   GET_MPRESS_KEY(&which);
  374.  
  375.   /* Hide window */
  376.   WindowHide(pw,NOEFFECT);
  377.  
  378.   /* Keep getting selections */
  379.   return BAR_CONTINUE;
  380. }
  381.  
  382.  
  383.  
  384. void pulldown_demo()
  385. {
  386.   int i;
  387.  
  388.  
  389.  /* Rewrite help window */
  390.   WindowClear(help);
  391.   WindowWriteCenterString(help,
  392.   "Press \030 \031 \032 \033 or use mouse to move bar,   ─┘ or hotkey to accept,   ESC to exit",1);
  393.  
  394.  /* Reassign functions of the bar menu entry */
  395.   for (i=0;i<=7;i++)
  396.     bar_items[i].func = NULLFN;
  397.  
  398.  /* Make special case for "Edit" entry */
  399.   bar_items[1].func = rt3;
  400.  
  401.  /* Reassign function for the menu_items1 POPUP_MENU_ENTRY */
  402.   for (i=0;i<=8;i++)
  403.     menu_items1[i].func = rt3;
  404.  
  405.  /* Create a BAR_MENU_PTR */
  406.   bar = BarCreateMenu(bar_items,  /* Array of BAR_MENU_ENTRY's   */
  407.               menu_colors,/* Pointer to menu colors      */
  408.               6,          /* Row to open bar menu window */
  409.               1,          /* Column to open bar menu window */
  410.           BARWRAP | BARSTATIC | BARMOUSE,
  411.               my_open);   /* Customized window open function */
  412.  
  413.  /* Now loop to create all pop-up menus
  414.     Be careful and asign NULL to the second pop-up menu */
  415.  
  416.   for (i=0; i<8; i++)       /* Loop for all pop-up menus */
  417.   {
  418.     if (pentry[i] != POPUP_ENTRY_NULL)  /* Skip over NULL pop-up window */
  419.     {
  420.       popups[i] =
  421.       PopupCreateMenu(pentry[i],   /* Array of POPUP_MENU_ENTRY's */
  422.               menu_colors, /* Array of menu colors,        */
  423.  
  424.             /* The next two arguments will later be ignored */
  425.             1,           /* Row to open pop-up window on */
  426.             1,           /* Column to open pop-up */
  427.  
  428.             height[i], /* height of the pop-up window   */
  429.                      /* No custom window open
  430.                         functions */
  431.       POPUPWRAP | POPUPSTATIC | POPUPMOUSE,
  432.             WNULLFN,
  433.             VWNULLFN);
  434.  
  435.     }
  436.     else
  437.       popups[i] = POPUP_NULL_PTR;   /* Assign NULL to second pop-up window */
  438.   }  /* end for (i=0 ... */
  439.   VirtualWriteString(POPUP_VIRTUAL_WINDOW(popups[7]),line,5,1);/* Draw horizontal line in virtual
  440.                             popup window popups[7] */
  441.  
  442.   PopupMakeEntryUnavailable(popups[6],2);   /* hide 2nd and 3rd entries in */
  443.   PopupMakeEntryUnavailable(popups[6],3);   /* popups[6]  */
  444.  
  445.  /* Now put bar and array of POPUP_MENU_PTR's together by calling
  446.     PulldownCreateMenu()    */
  447.   pull = PulldownCreateMenu(popups,bar,1);
  448.  
  449.  /* Call Pulldown Select function */
  450.   PulldownSelectMenu(pull,1,1,popuprank,popupstart,1);
  451.  
  452.  /* Dispose of pulldown menu and all other menus used to build it */
  453.   PulldownMenuFreeAll(pull);
  454. }
  455.  
  456.  
  457. int rt3(PULLDOWN_MENU_PTR p, BAR_MENU_PTR b, POPUP_MENU_PTR pop,
  458.     int hs, int vs)
  459. {
  460.   int ch;
  461.   WindowClear(pw);
  462.   WindowPrintf(pw,"You have selected bar option %s\n",
  463.                    BAR_ENTRY_STRING(b,hs));
  464.   if (pop != (POPUP_MENU_PTR)0)
  465.     WindowPrintf(pw,"Popup entry %s",POPUP_ENTRY_STRING(pop,vs));
  466.   WindowWriteCenterString(pw,"Press a key to continue...",3);
  467.   WindowDisplay(pw,1,NOEFFECT);
  468.   ch = GET_MPRESS_KEY(&which);
  469.   WindowHide(pw,NOEFFECT);
  470.   if (ch == ESC)
  471.     return PULLDOWN_CONTINUE;
  472.   return PULLDOWN_CONTINUE;
  473. }
  474.  
  475.  
  476.  
  477. WPOINTER my_open()          /* Custom window open function for bar menu */
  478. {
  479.   WPOINTER w;
  480.   w = WindowInitialize(NOBORDER,1,1,80,1,BLACKONWHITE,BLACKONWHITE,"");
  481.   WindowOpen(w);
  482.   return w;
  483. }
  484.  
  485.  
  486. int beep_it()
  487. {
  488.   MakeSound(100,200);         /* Produce error tone */
  489.   return DONT_PROCESS;
  490. }
  491.  
  492.  
  493.  
  494. void initialize_error()    /* Initialize error window and error handler */
  495. {
  496.   error_window =
  497.     WindowInitialize(BORDER,1,1,60,5,WHITEONRED,WHITEONRED,SINGLEBOX);
  498.   WindowOpen(error_window);
  499.   window_error_func = custom_error_func;  /* Assign error function */
  500. }
  501.  
  502.  
  503. void custom_error_func(int errcode, char *sourcefile, int sourceline,
  504.                char *funcname)
  505. {
  506.   int ch;
  507.   if (error_check_flag)
  508.   {
  509.     MakeSound(100,500);           /* Beep */
  510.     if (errcode == NO_HEAP_MEM)   /* Always check for the heap memory error
  511.                    separately */
  512.     {
  513.       VideoWriteString("You have ran out of Heap Memory",1,1);
  514.       VideoWriteString(
  515.          "Press 'Q' to quit program, Any other key to Continue...",2,1);
  516.     }
  517.     else                         /* Display error window */
  518.     {
  519.       WindowClear(error_window);
  520.       WindowDisplay(error_window,1,EXPLODE);
  521.       WindowPrintf(error_window,
  522.        "Error Code: %d\nSource File: %s\nSource Line %d\nError Occurred in %s",
  523.        errcode,sourcefile,sourceline,funcname);
  524.       WindowWriteString(error_window,
  525.     "Press 'Q' to quit program, Any other key to Continue...",5,1);
  526.     }
  527.     ch = GET_MPRESS_KEY(&which);
  528.     if (ch == 'Q' || ch == 'q')
  529.     {
  530.       VideoRestore(v);
  531.       exit(0);
  532.     }
  533.     else
  534.     if (errcode != NO_HEAP_MEM)
  535.       WindowHide(error_window,CONTRACT);
  536.   }
  537. }
  538.